package com.curry.android.util;

import android.os.Handler;
import android.os.Message;
import android.widget.TextView;

import java.lang.ref.WeakReference;

/**
 * 时分秒3个TextView的倒计时
 */
public class CountDownHMSHandler extends Handler {
//    private WeakReference<TextView[]> weakReference;
    private TextView sTv;
    private TextView mTv;
    private TextView hTv;
    private TextView[] tvs;
    /**
     * 倒计时总秒数
     */
    private int mSecond = 0;
    /**
     * 时间间隔
     */
    private int interval = 1000;
    private int mWhat;
    private boolean hasStop = false;
    private boolean mIsUseCustomCountDownInfo;

    public CountDownHMSHandler() {
    }

    public CountDownHMSHandler(TextView hTv, TextView mTv, TextView sTv, int what) {
        mWhat = what;
        this.hTv=hTv;
        this.mTv=mTv;
        this.sTv=sTv;
    }

    public void startTimer(int second) {
        startTimer(String.valueOf(second));
    }

    public void startTimer(String second) {
        hasStop = false;
        //TODO 对Handler的机制还是不了解，先这么写
        removeCallbacksAndMessages(null);
        setCountSecond(second);
        sendMessageDelayed(obtainMessage(mWhat), 1000);
    }

    public void stopTimer() {
        hasStop = true;
        removeCallbacksAndMessages(null);
    }

    /**
     * 将倒计时总秒数传入
     *
     * @param second 总秒数
     */
    public void setCountSecond(String second) {
        try {
            mSecond = Math.abs(Integer.parseInt(second));
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        setTimeToTextView();
    }

    public void isUseCustomCountDownInfo(boolean isUseCustomCountDownInfo) {
        this.mIsUseCustomCountDownInfo = isUseCustomCountDownInfo;
    }


    private void setTimeToTextView() {
        int secondInt = mSecond;
        int h = secondInt / 3600;
        int m = (secondInt - h * 3600) / 60;
        int s = secondInt % 60;
        if (mIsUseCustomCountDownInfo) {
            if (mListener != null) {
                mListener.onCountDown(h, m, s);
            }
        } else {
            if (mTv != null) {

                hTv.setText(change2(h));
                mTv.setText(change2(m));
                sTv.setText(change2(s));
            }
        }
    }
    private String change2(int s) {
        if (s<10) {
            return "0"+s;
        } else {
            return s+"";
        }
    }
    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        if (msg.what == mWhat) {
            if (mSecond > 0) {
                mSecond--;
                if (!hasStop) {
                    this.sendMessageDelayed(obtainMessage(mWhat), interval);
                } else {
                    if (mListener != null) {
                        mListener.onFinish();
                    }
                    return;
                }
                setTimeToTextView();
            } else {
                if (mListener != null) {
                    //CALLBACK TimeUp
                    mListener.onFinish();
                }
            }
        }
    }

    public boolean hasStop() {
        return hasStop;
    }


    private OnCountDownTimerFinishedListener mListener;

    public void setOnCountDownTimerFinishedListener(OnCountDownTimerFinishedListener listener) {
        mListener = listener;
    }

    //倒计时都是一致的，但是倒计时结束后的处理，是不一致的，所以将倒计时结束的事件外放，让使用者自行调用；
    public interface OnCountDownTimerFinishedListener {
        void onFinish();
        void onCountDown(int h, int m, int s);
    }
}
